// 弹弹球消砖块.cpp : 定义控制台应用程序的入口点。
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Width 680
#define High 480
#define Brick_num_rows 10 //游戏砖块的列数
#define Brick_num_lines 4 //游戏砖块的行数
//定义全局变量
char c;
int j, n = 0;
int key = 0;
int ball_x, ball_y; //小球的坐标
int ball_vx, ball_vy; //小球的速度
int radius; //小球的半径
int bar_x, bar_y; //挡板的中心位置
int bar_height, bar_width; //挡板的高度和宽度
int bar_left, bar_right, bar_top, bar_bottom; //挡板的上下左右位置坐标//砖块
int isBrickExisted[Brick_num_lines][Brick_num_rows]; //每个砖块是否存在,1为存在,0为没有了
int color1[Brick_num_lines][Brick_num_rows];
int color2[Brick_num_lines][Brick_num_rows];
int color3[Brick_num_lines][Brick_num_rows];
int brick_height, brick_width; // 砖块的高度和宽度

							   //数据初始化
void initData();
//创建黑色圆圈,防止绿色圆移动的时候带着尾巴.黑色圆就是用来打底(清除痕迹)
void clean();
//画其他颜色的圆
void show();

//与用户输入无关的更新
void updateWithoutInput();

//与用户更新相关的更新
void updateWithInput();

//游戏结束的函数
void gameOver();

int main()
{
		initData();
		while (key==0) 
		{
			clean();
			updateWithoutInput();
			updateWithInput();
			show();
		}
		gameOver();
}

void initData() {
	//初始化圆球的信息
	ball_x = Width / 2;
	ball_y = High / 2;
	ball_vx = 1;
	ball_vy = 1;
	radius = 20;
	//初始化挡板的信息
	//宽高 和中心坐标
	bar_height = High / 20;
	bar_width = Width / 5;
	bar_x = Width / 2;
	bar_y = High - bar_height / 2;
	//上下左右坐标
	bar_left = bar_x - bar_width / 2;
	bar_right = bar_x + bar_width / 2;
	bar_top = bar_y - bar_height / 2;
	bar_bottom = bar_y + bar_height / 2;

	//初始化砖块信息
	brick_width = Width / Brick_num_rows;
	brick_height = High / 10;
	int i;
	//设置默认所有砖块都存在
	for (j = 0; j<Brick_num_lines; j++)
	{
		for (i = 0; i<Brick_num_rows; i++) {
			isBrickExisted[j][i] = 1;
		}
	}
	//将0~255随机分配给三个二维数组
	srand((unsigned)time(NULL));
	for (j = 0; j<Brick_num_lines; j++)
	{
		for (i = 0; i<Brick_num_rows; i++)
		{
			color1[j][i] = rand() % 256;
		}
	}
	for (j = 0; j<Brick_num_lines; j++)
	{
		for (i = 0; i<Brick_num_rows; i++)
		{
			color2[j][i] = rand() % 256;
		}
	}
	for (j = 0; j<Brick_num_lines; j++)
	{
		for (i = 0; i<Brick_num_rows; i++)
		{
			color3[j][i] = rand() % 256;
		}
	}
	initgraph(Width, High);
	BeginBatchDraw();
}

void clean() {
	setcolor(BLACK);
	setfillcolor(BLACK);
	fillcircle(ball_x, ball_y, radius);
	bar(bar_left, bar_top, bar_right, bar_bottom);
	int i, brick_left, brick_right, brick_top, brick_bottom;
	for (j = 0; j<Brick_num_lines; j++)
	{
		for (i = 0; i<Brick_num_rows; i++) {
			brick_left = i*brick_width;
			brick_right = brick_left + brick_width;
			brick_top = j*brick_height;
			brick_bottom = brick_top + brick_height;
			//砖块被小球碰到,清空砖块
			if (!isBrickExisted[j][i]) {
				fillrectangle(brick_left, brick_top, brick_right, brick_bottom);
			}
		}
	}
}

void show() {
	//画球
	setcolor(YELLOW);
	setfillcolor(CYAN);
	fillcircle(ball_x, ball_y, radius);
	//画板
	setcolor(YELLOW);
	setfillcolor(RED);
	bar(bar_left, bar_top, bar_right, bar_bottom);
	int i, brick_left, brick_right, brick_top, brick_bottom;
	for (j = 0; j<Brick_num_lines; j++)
	{
		for (i = 0; i<Brick_num_rows; i++) {
			brick_left = i*brick_width;
			brick_right = brick_left + brick_width;
			brick_top = j*brick_height;
			brick_bottom = brick_top + brick_height;
			//砖块存在,绘制砖块
			if (isBrickExisted[j][i] == 1) {
				//根据数组的数决定颜色
				setcolor(WHITE);
				setfillcolor(RGB(color1[j][i], color2[j][i], color3[j][i]));
				fillrectangle(brick_left, brick_top, brick_right, brick_bottom);
			}
		}
	}
	FlushBatchDraw();
	Sleep(3);
}

void updateWithoutInput() {
	if (ball_y >= bar_top - radius&&ball_x >= bar_left + radius&&ball_x <= bar_right - radius)
	{
		ball_vy = -ball_vy;
	}
	//更新小圆的坐标
	ball_x = ball_x + ball_vx;
	ball_y = ball_y + ball_vy;
	if ((ball_x <= radius) || (ball_x >= Width - radius)) {
		ball_vx = -ball_vx;
	}
	if ((ball_y <= radius) || (ball_y >= High - radius)) {
		ball_vy = -ball_vy;
	}
	//判断小球是否和某个砖块碰撞
	int i, brick_left, brick_right, brick_bottom, brick_top;
	for (j = 0; j<Brick_num_lines; j++)
	{
		for (i = 0; i<Brick_num_rows; i++) {
			if (isBrickExisted[j][i]) {
				brick_left = i*brick_width;
				brick_right = brick_left + brick_width;
				brick_top = j*brick_height;
				brick_bottom = brick_top + brick_height;
				if ((ball_y <= brick_bottom + radius) && (ball_x >= brick_left) && (ball_x <= brick_right)) {
					isBrickExisted[j][i] = 0;
					n++;
					ball_vy = -ball_vy;
				}
			}
		}
	}
	//判断小球是否碰到底部
	if (ball_y == High - radius&&ball_x)
	{
		char result[] = "GAME OVER!\n你的得分为00！\n是否继续游戏？";
		result[21] += n / 10;
		if (n>10)
		{
			n = n % 10;
			result[22] += n;

		}
		if (MessageBoxA(NULL, result, "提示", MB_SYSTEMMODAL | MB_YESNO) == 6)
		{
			initData();
		}
		else
		{
			key = 1;
		}
	}
}
//用户输入处理
void updateWithInput() {
	char input;
	if (_kbhit()) {
		input = _getch();
		if (input == 'a' && bar_left>0) {
			bar_x = bar_x - 15;
			bar_left = bar_x - bar_width / 2;
			bar_right = bar_x + bar_width / 2;
		}
		//右移
		if (input == 'd' && bar_right<Width) {
			bar_x = bar_x + 15;
			bar_left = bar_x - bar_width / 2;
			bar_right = bar_x + bar_width / 2;
		}
	}
}

void gameOver() {
	EndBatchDraw();
	closegraph();
}